package com.u3k.mid;

import dalvik.system.DexClassLoader;
import java.lang.reflect.Array;
import java.util.Arrays;

public class DexUtils {
    static String a = "DexUtils";

    private static Object getDexElements(Object methodObject) {
        return ba.getFieldOfClass(methodObject, methodObject.getClass(), "dexElements");
    }

    private static Object mergeArray(Object obj, Object obj2) {
        Class componentType = obj.getClass().getComponentType();
        int length = Array.getLength(obj);
        int length2 = Array.getLength(obj2) + length;
        Object newInstance = Array.newInstance(componentType, length2);
        for (int i = 0; i < length2; i++) {
            if (i < length) {
                Array.set(newInstance, i, Array.get(obj, i));
            } else {
                Array.set(newInstance, i, Array.get(obj2, i - length));
            }
        }
        return newInstance;
    }

    public static void a(ClassLoader classLoader, String apkFileName, String soFileName) {//TODO
        LogUtils.LogError(a, "injectDexAtFirst");
        LogUtils.LogError(a, "injectDexAtFirst 1  classLoader=" + classLoader);
        LogUtils.LogError(a, "injectDexAtFirst 1  SystemClassLoader=" + classLoader.getParent());
        DexClassLoader dexClassLoader = new DexClassLoader(apkFileName, soFileName, null, classLoader);
        LogUtils.LogError(a, "injectDexAtFirst 2  dexClassLoader=" + dexClassLoader);
        Object a = getDexElements(getPathList(classLoader));
        LogUtils.LogError(a, "injectDexAtFirst 3  baseDexElements=" + Arrays.toString((Object[]) a));
        Object a2 = getDexElements(getPathList(dexClassLoader));
        LogUtils.LogError(a, "injectDexAtFirst 4  newDexElements=" + Arrays.toString((Object[]) a2));
        a = mergeArray(a2, a);
        LogUtils.LogError(a, "injectDexAtFirst 5  allDexElements=" + Arrays.toString((Object[]) a));
        Object b = getPathList(classLoader);
        LogUtils.LogError(a, "injectDexAtFirst 6  pathList=" + b);
        ba.setFieldOfClass(b, b.getClass(), "dexElements", a);
        LogUtils.LogError(a, "injectDexAtFirst 7  finish");
    }

    private static Object getPathList(Object classLoader) {
        return ba.getFieldOfClass(classLoader, Class.forName("dalvik.system.BaseDexClassLoader"), "pathList");
    }
}
